package com.qj.travel.controller;

import com.qj.travel.alivod.VodTool;
import com.qj.travel.db.model.VoteRecordModel;
import com.qj.travel.service.*;
import com.qj.travel.temp.OutputInfoTemp;
import com.qj.travel.temp.TotalInfoTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Admin on 2016/10/1.
 */
@Controller
@RequestMapping("/vodinfo")
public class VideoInfoController {

    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private TransInfoService transInfoService;
    @Autowired
    private SnapInfoService snapInfoService;
    @Autowired
    private VoteSettingService voteSettingService;
    @Autowired
    private VoteRecordService voteRecordService;

    @RequestMapping(value = "/insertVideoInfo",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String insertVideoInfo(HttpServletRequest request) {
//        VideoInfoTemp videoInfoTemp = videoData;
        VodTool vodTool = new VodTool();
        String msg = "";
//        VodJson vodJson = new VodJson();
        Date date = new Date();
        String upTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(date);
        TotalInfoTemp totalInfoTemp = vodTool.getTotalInfo();
        String videoName = request.getParameter("videoName");
        String videoIntro = request.getParameter("videoIntro");
        String videoClass = request.getParameter("videoClass");
        String userId = request.getParameter("userId");
//        totalInfoTemp.getVideoInfoModel().setVideoName(videoData.getVideoName());
//        totalInfoTemp.getVideoInfoModel().setVideoIntro(videoData.getVideoIntro());
//        totalInfoTemp.getVideoInfoModel().setVideoClass(videoData.getVideoClass());
//        vodJson.setTransOssUrl(totalInfoTemp.getTransInfoModel().getTransOssUrl());
//        vodJson.setSnapOrrUrl(totalInfoTemp.getSnapInfoModel().getSnapOssUrl());
        totalInfoTemp.getVideoInfoModel().setVideoName(videoName);
        totalInfoTemp.getVideoInfoModel().setVideoIntro(videoIntro);
        totalInfoTemp.getVideoInfoModel().setVideoClass(videoClass);
        totalInfoTemp.getVideoInfoModel().setUserId(userId);
        totalInfoTemp.getVideoInfoModel().setUpTime(upTime);
        totalInfoTemp.getTransInfoModel().setVideoClass(videoClass);
        totalInfoTemp.getTransInfoModel().setUserId(userId);
        totalInfoTemp.getTransInfoModel().setUpTime(upTime);
        totalInfoTemp.getSnapInfoModel().setVideoClass(videoClass);
        totalInfoTemp.getSnapInfoModel().setUserId(userId);
        totalInfoTemp.getSnapInfoModel().setUpTime(upTime);
        videoInfoService.insertVideoInfo(totalInfoTemp.getVideoInfoModel());
        transInfoService.insertTransInfo(totalInfoTemp.getTransInfoModel());
        snapInfoService.insertSnapInfo(totalInfoTemp.getSnapInfoModel());
//        return vodJson;
        if (totalInfoTemp.getTransInfoModel().getTransFailCode() != null) {
            msg = totalInfoTemp.getTransInfoModel().getTransFailCode();
        } else if (totalInfoTemp.getTransInfoModel().getTransFailCode() == null
                && totalInfoTemp.getTransInfoModel().getTransOssUrl() == null
                && totalInfoTemp.getSnapInfoModel().getSnapOssUrl() != null) {
            msg = "转码信息录入失败！";
        }

        if (totalInfoTemp.getSnapInfoModel().getSnapFailCode() != null) {
            msg = msg + "\r\n" + totalInfoTemp.getSnapInfoModel().getSnapFailCode();
        } else if (totalInfoTemp.getSnapInfoModel().getSnapFailCode() == null
                && totalInfoTemp.getSnapInfoModel().getSnapOssUrl() == null
                && totalInfoTemp.getTransInfoModel().getTransOssUrl() != null) {
            msg = "截图信息录入失败！";
        } else if (totalInfoTemp.getSnapInfoModel().getSnapFailCode() == null
                && totalInfoTemp.getSnapInfoModel().getSnapOssUrl() == null
                && totalInfoTemp.getTransInfoModel().getTransOssUrl() == null
                && totalInfoTemp.getTransInfoModel().getTransFailCode() == null) {
            msg = msg + "\r\n" + "截图信息录入失败！";
        }

        if (totalInfoTemp.getTransInfoModel().getTransOssUrl() != null
                && totalInfoTemp.getSnapInfoModel().getSnapOssUrl() != null) {
            msg = "视频已提交审核！";
        }

        return msg;
    }

    @RequestMapping(value = "/getUserVideo")
    @ResponseBody
    public OutputInfoTemp getUserVideo(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapByUser(userId));
        outputInfoTemp.setTransInfoModel(transInfoService.getTransByUser(userId));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoByUser(userId));
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getUserVideoPublished")
    @ResponseBody
    public OutputInfoTemp getUserVideoPublished(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapByUserPublished(userId));
        outputInfoTemp.setTransInfoModel(transInfoService.getTransByUserPublished(userId));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoByUserPublished(userId));
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getUserVideoNotPublished")
    @ResponseBody
    public OutputInfoTemp getUserVideoNotPublished(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapByUserNotPublished(userId));
        outputInfoTemp.setTransInfoModel(transInfoService.getTransByUserNotPublished(userId));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoByUserNotPublished(userId));
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getUserVideoNotPass")
    @ResponseBody
    public OutputInfoTemp getUserVideoNotPass(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransByUserNotPass(userId));
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapByUserNotPass(userId));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoByUserNotPass(userId));
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getVideoNotPass")
    @ResponseBody
    public OutputInfoTemp getVideoNotPass() {
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransNotPass());
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapNotPass());
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoNotPass());
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getOutputList")
    @ResponseBody
    public OutputInfoTemp getOutputList() {
//        int pageNow = Integer.parseInt(request.getParameter("pageNow"));
//        int pageSize = 4;
//        int startRow = pageSize * (pageNow - 1);
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransInfoList());
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapInfoList());
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoInfoList());
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getTenVideo")
    @ResponseBody
    public OutputInfoTemp getTenVideo(HttpServletRequest request) {
        int pageNow = Integer.parseInt(request.getParameter("pageNow"));
        int pageSize = 10;
        int startRow = pageSize * (pageNow - 1);
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransInfoListByPage(startRow,pageSize));
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapInfoListByPage(startRow,pageSize));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoInfoListByPage(startRow,pageSize));
        return outputInfoTemp;
    }

    @RequestMapping(value = "/getVideoBySort")
    @ResponseBody
    public OutputInfoTemp getVideoBySort(HttpServletRequest request) {
        String videoClass = request.getParameter("videoClass");
        int pageNow = Integer.parseInt(request.getParameter("pageNow"));
        int pageSize = 4;
        int startRow = pageSize * (pageNow - 1);
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransBySort(videoClass,startRow,pageSize));
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapBySort(videoClass,startRow,pageSize));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoBySort(videoClass,startRow,pageSize));
        return outputInfoTemp;
    }

    @RequestMapping(value = "getVideoPublished")
    @ResponseBody
    public OutputInfoTemp getVideoPublished(HttpServletRequest request) {
        int pageNow = Integer.parseInt(request.getParameter("pageNow"));
        int pageSize = 4;
        int startRow = pageSize * (pageNow - 1);
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransPublished(startRow,pageSize));
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapPublished(startRow,pageSize));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoPublished(startRow,pageSize));
        return outputInfoTemp;
    }

    @RequestMapping(value = "getVideoPublishedBySort")
    @ResponseBody
    public OutputInfoTemp getVideoPublishedBySort(HttpServletRequest request) {
        String videoClass = request.getParameter("videoClass");
        int pageNow = Integer.parseInt(request.getParameter("pageNow"));
        int pageSize = 4;
        int startRow = pageSize * (pageNow - 1);
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        if (videoClass.equals("all")) {
            outputInfoTemp.setTransInfoModel(transInfoService.getTransPublished(startRow,pageSize));
            outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapPublished(startRow,pageSize));
            outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoPublished(startRow,pageSize));
        } else {
            outputInfoTemp.setTransInfoModel(transInfoService.getTransPublishedBySort(videoClass,startRow,pageSize));
            outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapPublishedBySort(videoClass,startRow,pageSize));
            outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoPublishedBySort(videoClass,startRow,pageSize));
        }

        return outputInfoTemp;
    }

    @RequestMapping(value = "/vote",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String vote(HttpServletRequest request) {
        String msg = "";
        VoteRecordModel voteRecordModel = new VoteRecordModel();
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        String userId = request.getParameter("userId");
        String ip = request.getRemoteAddr();
        Date date = new Date();
        String voteTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(date);
        voteRecordModel.setVoteIp(ip);
        voteRecordModel.setUserId(userId);
        voteRecordModel.setVideoId(videoId);
        voteRecordModel.setVoteTime(voteTime);
        int userVoteNum = voteSettingService.selectUserVoteNum();
        int ipVoteNum = voteSettingService.selectIpVoteNum();
        int isPublish = videoInfoService.getVideoStatus(videoId);
        if (isPublish == 1) {
            if (userVoteNum != 0 && ipVoteNum != 0) {
                if (voteRecordService.selectUserNum(userId) < voteSettingService.selectUserVoteNum()
                        && voteRecordService.selectIpNum(ip) < voteSettingService.selectIpVoteNum()) {
                    int result = videoInfoService.updateVoteNum(videoId,1);
                    int result2 = voteRecordService.insertRecord(voteRecordModel);
                    if (result == 1) {
                        msg = "投票成功！";
                    } else {
                        msg = "投票失败！";
                    }
                } else if (voteRecordService.selectUserNum(userId) >= userVoteNum) {
                    msg = "该用户已达到投票上限！";
                } else if (voteRecordService.selectIpNum(ip) >= ipVoteNum) {
                    msg = "该IP已达到投票上限！";
                }
            } else if (userVoteNum != 0 && ipVoteNum == 0) {
                if (voteRecordService.selectUserNum(userId) < userVoteNum) {
                    int result = videoInfoService.updateVoteNum(videoId,1);
                    int result2 = voteRecordService.insertRecord(voteRecordModel);
                    if (result == 1) {
                        msg = "投票成功！";
                    } else {
                        msg = "投票失败！";
                    }
                } else if (voteRecordService.selectUserNum(userId) >= userVoteNum) {
                    msg = "该用户已达到投票上限！";
                }
            } else if (userVoteNum == 0 && ipVoteNum != 0) {
                if (voteRecordService.selectIpNum(ip) < ipVoteNum) {
                    int result = videoInfoService.updateVoteNum(videoId,1);
                    int result2 = voteRecordService.insertRecord(voteRecordModel);
                    if (result == 1) {
                        msg = "投票成功！";
                    } else {
                        msg = "投票失败！";
                    }
                } else if (voteRecordService.selectIpNum(ip) >= ipVoteNum) {
                    msg = "该IP已达到投票上限！";
                }
            } else {
                int result = videoInfoService.updateVoteNum(videoId,1);
                int result2 = voteRecordService.insertRecord(voteRecordModel);
                if (result == 1) {
                    msg = "投票成功！";
                } else {
                    msg = "投票失败！";
                }
            }

        }  else if (isPublish == 2){
            msg = "视频已锁定！";
        }

        return msg;
    }

    @RequestMapping(value = "/updateVideoInfo",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateVideoInfo(HttpServletRequest request) {
        int videoId = Integer.parseInt(request.getParameter("videoId"));
        String videoName = request.getParameter("videoName");
        String videoClass = request.getParameter("videoClass");
        String videoIntro = request.getParameter("videoIntro");

        if (videoInfoService.getVideoInfo(videoId) != null) {
            int result = videoInfoService.updateVideoInfo(videoId,videoName,videoIntro,videoClass);
            int result2 = snapInfoService.updateClass(videoId,videoClass);
            int result3 = transInfoService.updateClass(videoId,videoClass);
            if (result == 1 && result2 == 1 && result3 == 1) {
                return "修改成功！";
            } else {
                return "修改失败！";
            }
        } else {
            return "视频未找到！";
        }
    }

    @RequestMapping(value = "/getVideoNotPublished")
    @ResponseBody
    public OutputInfoTemp getVideoNotPublished(HttpServletRequest request) {
        int pageNow = Integer.parseInt(request.getParameter("pageNow"));
        int pageSize = 10;
        int startRow = pageSize * (pageNow - 1);
        OutputInfoTemp outputInfoTemp = new OutputInfoTemp();
        outputInfoTemp.setTransInfoModel(transInfoService.getTransNotPublished(startRow,pageSize));
        outputInfoTemp.setSnapInfoModel(snapInfoService.getSnapNotPublished(startRow,pageSize));
        outputInfoTemp.setVideoInfoModel(videoInfoService.getVideoNotPublished(startRow, pageSize));
        return outputInfoTemp;
    }

}
